package avicit.bdp.dds.server.builder;

import avicit.bdp.dds.dao.entity.ProcessDefinition;
import avicit.bdp.dds.dao.entity.ProcessInstance;
import avicit.bdp.dds.dao.entity.TaskInstance;
import avicit.bdp.dds.server.entity.DataxTaskExecutionContext;
import avicit.bdp.dds.server.entity.ProcedureTaskExecutionContext;
import avicit.bdp.dds.server.entity.SQLTaskExecutionContext;
import avicit.bdp.dds.server.entity.SqoopTaskExecutionContext;
import avicit.bdp.dds.server.entity.TaskExecutionContext;

/**
 * @author admin
 *  TaskExecutionContext builder
 */
public class TaskExecutionContextBuilder {

    public static TaskExecutionContextBuilder get(){
        return new TaskExecutionContextBuilder();
    }

    private final TaskExecutionContext taskExecutionContext =  new TaskExecutionContext();

    /**
     * build taskInstance related info
     *
     * @param taskInstance taskInstance
     * @return TaskExecutionContextBuilder
     */
    public TaskExecutionContextBuilder buildTaskInstanceRelatedInfo(TaskInstance taskInstance){
        taskExecutionContext.setTaskInstanceId(taskInstance.getId());
        taskExecutionContext.setTaskName(taskInstance.getName());
        taskExecutionContext.setStartTime(taskInstance.getStartTime());
        taskExecutionContext.setTaskType(taskInstance.getTaskType());
        taskExecutionContext.setLogPath(taskInstance.getLogPath());
        taskExecutionContext.setExecutePath(taskInstance.getExecutePath());
        taskExecutionContext.setTaskJson(taskInstance.getTaskJson());
        taskExecutionContext.setWorkerGroup(taskInstance.getWorkerGroup());
        taskExecutionContext.setHost(taskInstance.getHost());
        taskExecutionContext.setResources(taskInstance.getResources());
        taskExecutionContext.setNeedReturnResult(taskInstance.isNeedReturnResult());
        return this;
    }


    /**
     * build processInstance related info
     *
     * @param processInstance processInstance
     * @return TaskExecutionContextBuilder
     */
    public TaskExecutionContextBuilder buildProcessInstanceRelatedInfo(ProcessInstance processInstance){
        if(processInstance == null){
            return this;
        }
        taskExecutionContext.setProcessInstanceId(processInstance.getId());
        taskExecutionContext.setScheduleTime(processInstance.getScheduleTime());
        taskExecutionContext.setGlobalParams(processInstance.getGlobalParams());
        taskExecutionContext.setExecutorId(processInstance.getExecutorId());
        taskExecutionContext.setCmdTypeIfComplement(processInstance.getCmdTypeIfComplement());
        taskExecutionContext.setTenantCode(processInstance.getTenantCode());
        taskExecutionContext.setQueue(processInstance.getQueue());
        return this;
    }

    /**
     * build processDefinition related info
     *
     * @param processDefinition processDefinition
     * @return TaskExecutionContextBuilder
     */
    public TaskExecutionContextBuilder buildProcessDefinitionRelatedInfo(ProcessDefinition processDefinition){
        taskExecutionContext.setProcessDefineId(processDefinition.getId());
        taskExecutionContext.setProjectId(processDefinition.getProjectId());
        return this;
    }


    /**
     * build SQLTask related info
     *
     * @param sqlTaskExecutionContext sqlTaskExecutionContext
     * @return TaskExecutionContextBuilder
     */
    public TaskExecutionContextBuilder buildSQLTaskRelatedInfo(SQLTaskExecutionContext sqlTaskExecutionContext){
        taskExecutionContext.setSqlTaskExecutionContext(sqlTaskExecutionContext);
        return this;
    }


    /**
     * build DataxTask related info
     *
     * @param dataxTaskExecutionContext dataxTaskExecutionContext
     * @return TaskExecutionContextBuilder
     */
    public TaskExecutionContextBuilder buildDataxTaskRelatedInfo(DataxTaskExecutionContext dataxTaskExecutionContext){
        taskExecutionContext.setDataxTaskExecutionContext(dataxTaskExecutionContext);
        return this;
    }

    /**
     * build procedureTask related info
     *
     * @param procedureTaskExecutionContext procedureTaskExecutionContext
     * @return TaskExecutionContextBuilder
     */
    public TaskExecutionContextBuilder buildProcedureTaskRelatedInfo(ProcedureTaskExecutionContext procedureTaskExecutionContext){
        taskExecutionContext.setProcedureTaskExecutionContext(procedureTaskExecutionContext);
        return this;
    }

    /**
     * build sqoopTask related info
     *
     * @param sqoopTaskExecutionContext sqoopTaskExecutionContext
     * @return TaskExecutionContextBuilder
     */
    public TaskExecutionContextBuilder buildSqoopTaskRelatedInfo(SqoopTaskExecutionContext sqoopTaskExecutionContext){
        taskExecutionContext.setSqoopTaskExecutionContext(sqoopTaskExecutionContext);
        return this;
    }


    /**
     * create
     *
     * @return taskExecutionContext
     */
    public TaskExecutionContext create(){
        return taskExecutionContext;
    }

}
